package auctionhausen



import org.junit.*
import grails.test.mixin.*

@TestFor(CustomerController)
@Mock([Customer, Listing, Bid])
class CustomerControllerTests {


	def populateValidParams(params) {
		assert params != null
		params['email'] ='sanss002@test.umn.edu'
		params['password'] = 'PASSWORD'
	}

	void tearDown() {
		super.tearDown()
		Customer.deleteAll(Customer.getAll())
		Listing.deleteAll(Listing.getAll())
		Bid.deleteAll(Bid.getAll())
	}

	void testSave() {
		controller.customerService = [createCustomer : {return [hasErrors: {return true}]}];
		controller.save()

		assert model.customerInstance != null
		assert view == '/customer/create'

		response.reset()

		populateValidParams(params)
		controller.customerService = [createCustomer : {return [hasErrors: {return false}, id : 1]}];
		controller.save()

		assert response.redirectedUrl == '/customer/show/1'
		assert controller.flash.message != null
	}

	void testDeleteFailsFor_CustomerWithListings() {
		populateValidParams(params)

		def customer = new Customer(params)
		def listing = new Listing(
				name:"Harry Potter Koozie",
				endDate:(new Date()+1),
				startingPrice:4.5F,
				description:'Every harry potter fan\'s dream item. \nRepresent HP to the max while keeping your drink cold!!!',
				seller: customer)
		customer.addToListings(listing)
		customer.save()
		assert Customer.count() == 1
		assert Listing.count() == 1


		request.method = 'POST'
		params.id = customer.id

		controller.delete()

		assert Customer.get(customer.id) != null
		assert controller.flash.message != null
		assert controller.flash.message == 'customer.not.deleted.message'
		assert response.redirectedUrl == "/customer/show/${customer.id}"
	}

	void testDeleteFailsFor_CustomerWithBids() {
		populateValidParams(params)

		def customer = new Customer(params)
		def listing = new Listing(
				name:"Harry Potter Koozie",
				endDate:(new Date()+1),
				startingPrice:4.5F,
				description:'Every harry potter fan\'s dream item. \nRepresent HP to the max while keeping your drink cold!!!',
				seller: customer)
		customer.addToListings(listing)
		customer.save()
		assert Customer.count() == 1
		assert Listing.count() == 1

		def bidder = new Customer(email:'bidder@auctionhausen.net',
				password:'bidder')
		bidder.save()
		assert Customer.count() == 2

		listing.addToBids(new Bid(amount:5.0F, bidder:bidder))
		listing.save()
		assert Bid.count() == 1
		request.method = 'POST'
		params.id = bidder.id

		controller.delete()

		assert Customer.get(bidder.id) != null
		assert controller.flash.message != null
		assert controller.flash.message == 'customer.not.deleted.message'
		assert response.redirectedUrl == "/customer/show/${bidder.id}"
	}

	void testDeletePassesFor_CustomerWithNoBidsAndNoListings() {
		populateValidParams(params)

		def customer = new Customer(params)
		customer.save()
		assert Customer.count() == 1

		request.method = 'POST'
		params.id = customer.id

		controller.delete()

		assert Customer.get(customer.id) == null
		assert response.redirectedUrl == "/customer/list"
	}
}
